package com.wlr.littleprogram.spider.processor;

import com.wlr.littleprogram.entity.CpuInfo;
import com.wlr.littleprogram.service.SpiderCpuInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lrwang
 * @version 1.0
 * @date 2022/7/22 17:59
 */
@Slf4j
@Component
public class ZolCpuPageProcessor implements PageProcessor {
    private Site site = Site.me().setRetryTimes(3).setSleepTime(100);
    @Override
    public void process(Page page) {
        Html html = page.getHtml();
        page.putField("brand", html.xpath("/html/body/div[4]/div[1]/div[8]/ul/li/h3/a/text()").all().toString());
        page.putField("title", html.xpath("/html/body/div[4]/div[1]/div[8]/ul/li/h3/a/span").all().toString());
        page.putField("title1", html.xpath("/html/body/div[4]/div[1]/div[8]/ul/li/h3/a/@title").all().toString());
        page.putField("price", html.xpath("/html/body/div[4]/div[1]/div[8]/ul/li/div/span/b/text()").all().toString());
        try {
            page.addTargetRequests(html.links().regex("https://detail.zol.com.cn/cpu/s6219/[0-9]+.html").all());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //*[@id="J_PicMode"]/li[2]
    @Override
    public Site getSite() {
        return site;
    }
}
